package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.bean.FollowListBean;
import com.yuejian_06.yuejian_06.bean.OrderCourseBean;
import com.yuejian_06.yuejian_06.bean.OrderListBean;
import com.yuejian_06.yuejian_06.bean.PostListBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.bean.UserInfoBean;
import com.yuejian_06.yuejian_06.bean.UserUpdateBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.service.MeService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by __ZL__ on 2017/9/25.
 */

public class MeModel{
    private MeService  meService;
    private Retrofit retrofit;
    public MeModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //用户详情
    public void getUserInfo(String user_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<UserInfoBean> call=meService.getUserById(user_id);
        call.enqueue(new Callback<UserInfoBean>() {
            @Override
            public void onResponse(Call<UserInfoBean> call, Response<UserInfoBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<UserInfoBean> call, Throwable t) {
                meListener.onFail("我的页面失败!");
            }
        });
    }

    //修改用户信息
    public void UpdateUser(String user_id,String user_name,String user_pwd,String phonenum,String address, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<UserUpdateBean> call=meService.UpdateUser(user_id,user_name,user_pwd,phonenum,address);
        call.enqueue(new Callback<UserUpdateBean>() {
            @Override
            public void onResponse(Call<UserUpdateBean> call, Response<UserUpdateBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<UserUpdateBean> call, Throwable t) {
                meListener.onFail("Model UpdateUser失败!");
            }
        });
    }

    //帖子列表
    public void getPostList(String user_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<List<PostListBean>> call=meService.getPostList(user_id);
        call.enqueue(new Callback<List<PostListBean>>() {
            @Override
            public void onResponse(Call<List<PostListBean>> call, Response<List<PostListBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<PostListBean>> call, Throwable t) {
                meListener.onFail("帖子列表Model失败!");
            }
        });
    }

    //订单列表
    public void getOrderList(String user_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<List<OrderListBean>> call=meService.getOrderList(user_id);
        call.enqueue(new Callback<List<OrderListBean>>() {
            @Override
            public void onResponse(Call<List<OrderListBean>> call, Response<List<OrderListBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<OrderListBean>> call, Throwable t) {
                meListener.onFail("订单列表Model失败!");
            }
        });
    }
    public void getOrderCourseList(String user_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<List<OrderCourseBean>> call=meService.getOrderCourseList(user_id);
        call.enqueue(new Callback<List<OrderCourseBean>>() {
            @Override
            public void onResponse(Call<List<OrderCourseBean>> call, Response<List<OrderCourseBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<OrderCourseBean>> call, Throwable t) {
                meListener.onFail("订单列表Model失败!");
            }
        });
    }

    //关注列表
    public void getFollowList(String user_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<List<FollowListBean>> call=meService.getFollowList(user_id);
        call.enqueue(new Callback<List<FollowListBean>>() {
            @Override
            public void onResponse(Call<List<FollowListBean>> call, Response<List<FollowListBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<FollowListBean>> call, Throwable t) {
                meListener.onFail("关注列表Model失败!");
            }
        });
    }

    //取消关注
    public void getDelFollow(String follow_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<SuccessBean> call=meService.getDelFollow(follow_id);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                meListener.onFail("取消关注Model失败!");
            }
        });
    }

    //申请退款Model
    public void getDelOrder(String sign_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<SuccessBean> call=meService.getDelOrder(sign_id);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                meListener.onFail("申请退款Model失败!");
            }
        });
    }

    //删除帖子
    public void getDelPost(String post_id, final MeListener meListener) {
        meService=retrofit.create(MeService.class);
        Call<SuccessBean> call=meService.getDelPost(post_id);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                meListener.onFail("申请退款Model失败!");
            }
        });
    }









}
